#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
hi.py - Perform Greetings
"""

import random, time, re
from datetime import datetime
from core.ircfuncs import irc_lower
field = "myself"

salutation= ur"(?#re)(?i)^(hey|hello|welcome|hi|ciao|aloha|buon giorno|bon dia|hola|salve|kaixo|salut)(:|,)? ($me|a tutti|a tots|a todos|a tot el m[óo]n|a todo el mundo)!?"
thanksbot = ur'(?#re)(?i)^(thanks|cheers|ta|thank you|gr[áà]ci[ae]s),? $me[!.]?$'

def wayf(bot, evt, cmd):
	time.sleep(random.randint(1,5))
	if cmd.dict.has_key("en"):
		answer = "I'm from"
	elif cmd.dict.has_key("cat"):
		answer = u"sóc de"
	elif cmd.dict.has_key("cast"):
		answer = "soy de"
	answer += " %s" % bot.on_server
	bot.reply(evt, answer)
	return True
wayf.rule = re.compile("(?#re)^(?P<en>where are you from(?: $me)?\??)$" )
wayf.fullmsg = True
wayf.chanlang =	{
	"ca": re.compile(ur"(?#re)(?P<cat>d'on sou|^¿?(?:$me[,:] )?d'on (?:ets|eres)(?:,? $me)?)\??$"),
	"es": re.compile(ur"(?#re)^¿?(?:$me[,:] )?(?P<cast>de d[óo]nde (?:sois|eres|s[oó]s))(?:,? $me)?\??$")
}
wayf.priority = "high"

def hi_(bot, evt, cmd):
	hello=cmd.groups[0]
	end = random.choice([' :) ', '!', '', '.'])
	reply_msg=random.choice(['%s $nick%s' % (hello, end), '$nick: %s%s%s' % (hello[0].upper(), hello[1:], end), hello, "%s%s" % (hello, end)])
	reply_msg=reply_msg.replace("welcome", "thanks")
	time.sleep(random.randint(0,10))
	bot.reply(evt, reply_msg)
	return True
hi_.rule = re.compile(salutation)
hi_.fullmsg = True
hi_.priority = "high"

def exclaim_(bot, evt, cmd):
	bot.reply(evt, "$nick!")
	return True
exclaim_.rule = re.compile(r"(?i)^$me! *$")
exclaim_.fullmsg = True

def empty_(bot, evt, cmd):
	bot.reply(evt, "$nick: what do you want?")
	return True
empty_.rule = re.compile(r"(?i)^$me(?:: ?)(?:\.{3})?$",)
empty_.fullmsg = True

def thanks_(bot, evt, cmd):
	bot.choose_reply(evt, bot.replies.thanks, 99)
	return True
thanks_.rule = re.compile(thanksbot)
thanks_.fullmsg = True

def reffered_(bot, evt, cmd):
	if irc_lower(evt.source) == "nickserv": return True
	hi=salutation.replace("$me", bot.current_nname)
	hi = re.match(hi, evt.msg)
	thanx = thanksbot.replace("$me", bot.current_nname)
	ta = re.match(thanx, evt.msg)
	if hi or ta: return
	bot.choose_reply(evt, bot.replies.tuttifruti, 4)
reffered_.rule = re.compile(r"(?i)^(?:($me)[^:].+?|.+?\b($me)[\.!?]?|.+?\b($me)\b.+?)$")
reffered_.fullmsg = True
reffered_.priority="low"

def where(bot, evt, cmd):
	path = bot.on_server
	if cmd.arguments:
		params=cmd.arguments.strip()
		if params.endswith(".pyc"): params=params.replace(".pyc",".py")
		if not params.endswith(".py"): params+=".py"
		params=params.replace("c.","commands/").replace("commands.","commands/")
		path+=params+"?ctype=text/html"
	bot.reply(evt, path)
where.rule="where"

def when(bot, evt, cmd):
	bot.reply(evt, "I was initialized on %s" % (datetime.fromtimestamp(bot.init_time).strftime("%Y-%m-%d %H:%M:%S")))
when.rule="when"

def chrono(bot, evt, cmd):
	print cmd.arguments
	if not cmd.arguments:
		network = bot.features.network()
		t = bot.init_time
		diff = timedelta(datetime.now(), datetime.fromtimestamp(t))
		diff_str = "%i d, %.2i h, %.2i m, %.2i s." % (
			diff.days,
			diff.hours,
			diff.minutes,
			diff.seconds
		)
		bot.reply(evt,
			"I've joined %s at %s (%s ago)",
			network,
			datetime.fromtimestamp(t).strftime("%Y-%m-%d %H:%M:%S"),
			diff_str
		)
	elif cmd.arguments == "wm":
		b = bot.bots(("slave", 1))
		t = b.init_time
		diff = timedelta(datetime.now(), datetime.fromtimestamp(t))
		diff_str = "%i d, %.2i h, %.2i m, %.2i s." % (
			diff.days,
			diff.hours,
			diff.minutes,
			diff.seconds
		)
		bot.reply(evt,
			"I've joined %s at %s (%s ago)",
			b.features.network(),
			datetime.fromtimestamp(t).strftime("%Y-%m-%d %H:%M:%S"),
			diff_str
		)
chrono.rule="chrono"
chrono.limit = 10

def nickserv_(bot, evt, cmd):
	if irc_lower(evt.source) == "nickserv":
		bot.ctrl_msg(evt, evt.msg)
		return True
nickserv_.rule = re.compile("(.*)")
nickserv_.evttypes = ["privnotice"]
nickserv_.fullmsg = True
nickserv_.priority = "high"
